﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class MediaDao : AbstractDao<Media>, IMediaDao
    {
        public override bool
        update(Media media)
        {
            try
            {
                var curr = getById(media.mId);
        
                curr.mName = media.mName;
                curr.mDescription = media.mDescription;
                curr.mLocation = media.mLocation;
                curr.mType = media.mType;

                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override Media
        getById(object id)
        {
            return getAll().Where(i => i.mId == (int)id).SingleOrDefault();
        }

        public List<Media>
        getByName(string mName)
        {
            try
            {
                var q = from m in _ctx.Medias
                        where m.mName.Contains(mName)
                        select m;
                return q.ToList<Media>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Media>
        getByType(string mType)
        {
            try
            {
                if (mType.Equals("photo") || mType.Equals("audio") || mType.Equals("video"))
                {
                    var q = from m in _ctx.Medias
                            where m.mType.Equals(mType)
                            select m;
                    return q.ToList<Media>();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Media>
        getByCreatedDate(DateTime mDateCreate)
        {
            try
            {
                var q = from m in _ctx.Medias
                        where m.mDateCreate.Equals(mDateCreate)
                        select m;
                return q.ToList<Media>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Media>
        getByCreatedDate(int yyyy, int mm, int dd)
        {
            try
            {
                var q = from m in _ctx.Medias
                        where m.mDateCreate.Year == yyyy &&
                              m.mDateCreate.Month == mm &&
                              m.mDateCreate.Day == dd
                        select m;
                return q.ToList<Media>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Media>
        getByUploader(string uploader)
        {
            try
            {
                var q = from m in _ctx.Medias
                        where m.mUploadedBy.Equals(uploader)
                        select m;
                return q.ToList<Media>();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region IMediaDao

    public interface IMediaDao : IDao<Media>
    {
        /// <summary>
        /// Select list of media info based on Name
        /// </summary>
        /// <param name="mName"></param>
        /// <returns></returns>
        List<Media> getByName(String mName);

        /// <summary>
        /// Select list of media info based on Type
        /// </summary>
        /// <param name="mType"></param>
        /// <returns></returns>
        List<Media> getByType(String mType);

        /// <summary>
        /// Select list of media info based on created date
        /// </summary>
        /// <param name="mCreateDate"></param>
        /// <returns></returns>
        List<Media> getByCreatedDate(DateTime mDateCreate);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="yyyy"></param>
        /// <param name="mm"></param>
        /// <param name="dd"></param>
        /// <returns></returns>
        List<Media> getByCreatedDate(int yyyy, int mm, int dd);

        /// <summary>
        /// Select list of media info based on employeeid
        /// </summary>
        /// <param name="uploader"></param>
        /// <returns></returns>
        List<Media> getByUploader(String uploader);
    }
    #endregion
}